package com.park.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.IntegralRecord;
import com.park.model.IntegralRecordWeb;
import com.park.model.IntegralRule;
import com.park.model.Paging;
import com.park.service.IntegralRecordService;
import com.park.service.IntegralRuleService;
import com.park.service.Utility;

@Controller
@RequestMapping("integral")
public class IntegralRecordController {
	@Autowired
	private IntegralRecordService integralRecordService;
	@Autowired
	private IntegralRuleService integralRuleService;
	private static Log logger = LogFactory.getLog(IntegralRecordController.class);
	
	@RequestMapping(value="insertIntegralDetail",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insertIntegralDetail(@RequestBody IntegralRecord integralRecord) {
		logger.info("[insertIntegralDetail]参数:"+integralRecord);
		int count = integralRecordService.insert(integralRecord);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(count > 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "添加成功");
		}else {
			resultMap.put("status", 1002);
			resultMap.put("message", "添加失败");
		}
		return Utility.gson.toJson(resultMap);
	}
	@RequestMapping(value="selectIntegralDetail",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectIntegralDetail(@RequestBody IntegralRecordWeb integralRecordWeb) {
		logger.info("[selectIntegralDetail]参数:"+integralRecordWeb);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		int size = integralRecordWeb.getStartPage();
		List<IntegralRecord> integralRecords = integralRecordService.selectIntegralDetail(integralRecordWeb);
		int count = integralRecordService.selectAllCount(integralRecordWeb);
		Paging page = new Paging(size,integralRecordWeb.getPageSize(),count,integralRecords);
		resultMap.put("status", 1001);
		resultMap.put("body", page);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
	@RequestMapping(value="insertIntegralRule",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insertIntegralRule(@RequestBody IntegralRule integralRule) {
		logger.info("[insertIntegralRule]参数:"+integralRule);
		int count = integralRuleService.insert(integralRule);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(count > 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "添加成功");
		}else {
			resultMap.put("status", 1002);
			resultMap.put("message", "添加失败");
		}
		return Utility.gson.toJson(resultMap);
	}

	@RequestMapping(value="selectIntegraRule",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String selectIntegraRule(@RequestBody IntegralRecordWeb integralRecordWeb) {
		logger.info("[selectIntegralDetail]参数:"+integralRecordWeb);
		int size = integralRecordWeb.getStartPage();
		Map<String,Object> resultMap = new HashMap<String, Object>();
		List<IntegralRule> integralRules = integralRuleService.selectIntegralRule(integralRecordWeb);
		int count = integralRuleService.selectAllCount(integralRecordWeb);
		Paging page = new Paging(size,integralRecordWeb.getPageSize(),count,integralRules);
		resultMap.put("status", 1001);
		resultMap.put("body", page);
		resultMap.put("message", "查询成功");
		return Utility.gson.toJson(resultMap);
	}
}
